/*
 * Copyright (C) 2009 Chang Min Lee <ch5ngmn@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. (/COPYING)
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "IoSessionAdminServerCreator.h"
#include "Session_i.h"
#include "ace/Arg_Shifter.h"

int 
IoSessionAdminServerCreator::open (int argc, ACE_TCHAR *argv[])
{
	if (0 == IoServerCreator::open (argc, argv)) 
  {
		this->server_name_ = SESSION_ADMIN_SERVER_NAME ;

    return 0;
  }
  return -1;
}

void IoSessionAdminServerCreator::close()
{
}

void 
IoSessionAdminServerCreator::activate_poa (PortableServer::POA_ptr parent_poa)
{
	CORBA::PolicyList policies (5);
	policies.length (5);
    
	// ID Assignment Policy
	policies[0] =    
		parent_poa->create_id_assignment_policy (PortableServer::USER_ID);

	// Lifespan Policy
	policies[1] =    
		parent_poa->create_lifespan_policy (PortableServer::PERSISTENT);

  // Request Processing Policy  
	policies[2] =    
		parent_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
    
	// Servant Retention Policy 
	policies[3] =     
		parent_poa->create_servant_retention_policy (PortableServer::RETAIN);

	// Id Uniqueness Policy 
	policies[4] =    
		parent_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);

  // Create the POA with these policies
  poa_ = parent_poa->create_POA(SESSION_ADMIN_POA_NAME,
																							 parent_poa->the_POAManager(), 
																							 policies);
	ACE_ASSERT (false == CORBA::is_nil(poa_.in()));

  // Destroy the policy objects
  for (CORBA::ULong i = 0; i != policies.length(); ++i) {
    policies[i]->destroy();
  }

	ACE_DEBUG((LM_DEBUG, "POA (%s) is activated\n", poa_->the_name()));
}

void
IoSessionAdminServerCreator::activate_server (CosNaming::NamingContextExt_ptr nctx)
{
	ACE_ASSERT (false == CORBA::is_nil(nctx));
	ACE_NEW_NORETURN (adminServer_, Taonet_SessionAdmin_i(session_manager_));

	// Obtain the RootPOA.
  CORBA::Object_var obj = orb_->resolve_initial_references ("RootPOA");

  // Narrow Object reference to RootPOA to a POA reference.
  PortableServer::POA_var root_poa = PortableServer::POA::_narrow (obj.in());

  PortableServer::POA_ptr session_poa = root_poa->find_POA (SESSION_POA_NAME, 0);
	ACE_ASSERT (false == CORBA::is_nil (session_poa));

	adminServer_->poa(session_poa);

	//Default servant
  poa_->set_servant(adminServer_);

	PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId (server_name_.c_str());
	poa_->activate_object_with_id (oid.in (), adminServer_);
  obj = poa_->id_to_reference (oid.in());
	//obj = poa_->servant_to_reference (adminServer_);
  CosNaming::Name_var name = nctx->to_name (server_name_.c_str()); 
	nctx->rebind (name, obj );
	
	ACE_DEBUG((LM_DEBUG, "Server (%s) is activated\n", server_name_.c_str()));
}

//ACE_FACTORY_DEFINE (DOS_CORE, IoSessionAdminServerCreator)